package com.mobimap.nhom9.gui;

import java.util.Collections;
import java.util.List;


import com.mobimap.nhom9.bus.ISearchEngine;
import com.mobimap.nhom9.bus.MobimapProvider;
import com.mobimap.nhom9.dto.LocationDTO;
import com.mobimap.nhom9.dto.PlaceExDTO;
import com.mobimap.nhom9.gui.utls.PlaceExAdapter;


import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class PlaceResultActivity extends AbstractActivity {

	private ListView listviewPlaces;
	@Override
	protected void onProcess(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		this.listviewPlaces = (ListView) this.findViewById(R.id.listviewPlaceResult);
		//Get Data from Bundle
		Bundle mBundle = getIntent().getExtras();
		Log.e("GUI","i = "+getIntent().toString());
		String query = mBundle.getString("search_type");
		List<PlaceExDTO> lstPlaces = Collections.emptyList();
		ISearchEngine searchEngine = MobimapProvider.getSearchEngine(this);
		if (query.equalsIgnoreCase("dia_diem"))	{
			String keyword = mBundle.getString("keywork");
			lstPlaces = searchEngine.timDiaChi(keyword);
			this.setKeyword(keyword);
		}
		else if (query.equalsIgnoreCase("dich_vu_xung_quanh"))	{
			String keyword = mBundle.getString("keywork");
			double latitude = mBundle.getDouble("latitude");
			double longitude = mBundle.getDouble("longitude");
			LocationDTO lcationDto = new LocationDTO(latitude, longitude);
			lstPlaces = searchEngine.timDichVuXungQuanh(lcationDto, keyword, 10);
		}
		this.refreshListAdapter(lstPlaces);
	}
	@Override
	protected int getLayout() {
		// TODO Auto-generated method stub
		return R.layout.activity_placeresult;
	}
	private void refreshListAdapter(final List<PlaceExDTO> lstPlaces) {
		PlaceExAdapter placeExAdapter = new PlaceExAdapter(this, lstPlaces);
		if (placeExAdapter != null)	{
			listviewPlaces.setAdapter(placeExAdapter);
			listviewPlaces.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
					PlaceExDTO placeDto = lstPlaces.get(arg2);
					Log.e("GUI", "click item: " + placeDto.toString());
					id = placeDto.getId();
					place_type = placeDto.getPlaceExType().toString().toLowerCase();
					isMark = true;
					finish();
				}
				
			});
		}
	}
	
	
}
